﻿//======================================================
//==     (c)2008 aspxcms inc by NeTCMS v1.0              ==
//==          Forum:bbs.aspxcms.com                   ==
//==         Website:www.aspxcms.com                  ==
//======================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NetCMS.Content;
using System.Xml;
using NetCMS.Config;
using System.Net;
using System.IO;

public partial class manage_ChangePassword : NetCMS.Web.UI.UserPage
{
    Ghistory inf = new Ghistory();
    protected void Page_Load(object sender, EventArgs e)
    {

        Response.Expires = 0;

        if (!IsPostBack)
        {
            Response.CacheControl = "no-cache";

        }
    }
    protected void saveSumbit(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string UserNum = NetCMS.Global.Current.UserNum;
            string oldpass = Request.Form["oPass"].ToString();
            string newPass = Request.Form["newPass"];
            string pnewPass = Request.Form["pnewPass"];
            string MD = NetCMS.Common.Input.MD5(oldpass, true);
            string MD2 = NetCMS.Common.Input.MD5(newPass, true);
            if (oldpass == "" || newPass == "" || newPass.Length < 3)
            {
                PageError("填写完整。<li>或者密码太短密码不能小于3位</li>", "");
            }
            if (newPass != pnewPass)
            {
                PageError("两次密码不一致。", "");
            }
            else
            {
                if (inf.sel_sysUserInfo(UserNum, MD) == 0)
                {
                    PageError("原始密码错误。", "");
                }
                else
                {
                    if (inf.update_userInfos(MD2, UserNum, 1) == 0)
                    {
                        PageError("意外错误。", "");
                    }
                    else
                    {
                        //#region 整合Discuz!NT
                        //XmlDocument xmlDoc = new XmlDocument();
                        //string xmlName = Server.MapPath("../../api/dz/Adapt.config");
                        //AdaptConfig adConfig = new AdaptConfig(xmlName);

                        //if (adConfig.isAdapt)
                        //{
                        //    string adaptePath = adConfig.adaptPath;
                        //    adaptePath += "?username=" + NetCMS.Global.Current.UserName + "&password=" + newPass + "&tag=change";
                        //    //PageRight("修改密码成功", "ChangePassword.aspx", adaptePath, adaptPrams);

                        //    Uri uri = new Uri(adaptePath);
                        //    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                        //    request.KeepAlive = false;
                        //    request.ProtocolVersion = HttpVersion.Version10;
                        //    request.Method = "GET";
                        //    request.ContentType = "application/x-www-form-urlencoded";
                        //    request.Proxy = System.Net.WebProxy.GetDefaultProxy();
                        //    request.AllowAutoRedirect = true;
                        //    request.MaximumAutomaticRedirections = 10;
                        //    request.Timeout = (int)new TimeSpan(0, 0, 1).TotalMilliseconds;
                        //    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
                        //    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                        //    //Stream responseStream = response.GetResponseStream();
                        //    //StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.Default);
                        //    //readStream.ReadToEnd();
                        //}
                        //#endregion

                        #region 动易，动网，Oblog 整合
                        NetCMS.API.PDO.PDOAction pdo = new NetCMS.API.PDO.PDOAction();
                        if (pdo.Enabled())
                        {
                            user u = new user();
                            NetCMS.Model.User ui = u.UserInfo(NetCMS.Global.Current.UserNum, 0);
                            int pdoSex = 1;
                            if (ui.Sex == 1)
                                pdoSex = 0;
                            if (ui.Sex == 2)
                                pdoSex = 1;                          
                            
                            pdo.UpdateUser(ui.UserName, newPass, "", ui.Email, ui.PassQuestion, ui.RealName, "", ui.UserFace, pdoSex.ToString());
                        }
                        #endregion

                        #region 动易，动网，Oblog 整合 重新登陆
                        if (pdo.Enabled())
                        {
                            user u = new user();
                            NetCMS.Model.User ui = u.UserInfo(NetCMS.Global.Current.UserNum, 0);
                            pdo.Login(ui.UserName, newPass, 1);
                        }
                        #endregion

                        PageRight("修改密码成功", "ChangePassword.aspx");
                    }
                }
            }
        }
    }
}
